const mysql = require('mysql2')
const MY_SQL_CONFIG = require('./conf/db')

console.log(MY_SQL_CONFIG, '---- process.env.NODE_ENV 04 -----')
const con = mysql.createConnection(MY_SQL_CONFIG)

con.connect((err) => {
  console.log('Connected to MySQL')
})
const addSql = `insert into user(userName,password,state) values('chienli3','123456',1)`
const searchSql = `select id,userName,password,state from user where userName like '%andy%' order by id desc` //%模糊查询% 倒序模糊查询包含andy的用户
const searchSql2 = `select id,userName,password from user`
const updateSql = `update user set state=0 where userName='andy'`
const deleteSql = `delete from user where id=11`

const sqlQuery = (sql) => {
  return new Promise((resolve, reject) => {
    con.query(sql, (err, data) => {
      if (err) reject(err)
      resolve(data)
    })
  })
}
module.exports = {
  sqlQuery,
}

// 由于只会创建一次 sql连接，保持长连即可，不需要关闭 con.end()
